package com.house365.core.thirdpart.auth.qqwb;

import android.os.Bundle;
import android.webkit.WebView;

import com.house365.core.thirdpart.auth.AppKeyInfo;
import com.house365.core.thirdpart.auth.AuthHandler;
import com.house365.core.thirdpart.auth.URLUtil;
import com.house365.core.thirdpart.auth.ThirdPartAuthListener;
import com.house365.core.thirdpart.auth.dto.AccessToken;

/**
 *@author py
 *@date 2012-11-6
 *@comment
 */
public class QQAuthHandler extends AuthHandler {

	public QQAuthHandler(AppKeyInfo appKeyInfo,
			ThirdPartAuthListener weiboAuthListener) {
		super(appKeyInfo, weiboAuthListener);
	}

	@Override
	public boolean handleAuth(WebView view, String url) {
		if (url.startsWith(appKeyInfo.getRedirect_uri())) {
			Bundle values = URLUtil.parseUrl(url);

			String error = values.getString("error");
			String error_code = values.getString("error_code");

			if (error == null && error_code == null) {
//				 mListener.onComplete(values);
				AccessToken accessToken  = new AccessToken(values);
				accessToken.setTokenSecret(appKeyInfo.getApp_secret());
				accessToken.setOpenid(values.getString(AccessToken.OPENID));
				weiboAuthListener.onComplete(accessToken, values);
				// 成功
			} else if (error.equals("access_denied")) {
				// 用户或授权服务器拒绝授予数据访问权限
				weiboAuthListener.onAccessDenied();
				// 拒绝访问
			} else {
				// 异常
				// mListener.onWeiboException(new WeiboException(error,
				// Integer.parseInt(error_code)));
				weiboAuthListener.onAuthException(values);
			}
			view.stopLoading();
			return true;
		}
		return false;
	}

}
